package com.tools.developer;

import java.io.ByteArrayInputStream;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.Signature;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.tools.developer.app.AppDetailActivity4Running;
import com.tools.developer.signature.SignatureUtil;
import com.tools.developer.util.CommonUtils;
import com.tools.developer.util.Md5Helper;

public class SignatureActivity extends Activity implements OnClickListener {
	private String mPkgName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signature);
		if (getIntent() != null) {
			Intent intent = getIntent();
			mPkgName = intent.getStringExtra(AppDetailActivity4Running.PKG_NAME);
			getSignature(this.getApplicationContext(), mPkgName);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		default:
			break;
		}
	}

	public void getSignature(Context context, String pkgName) {
		Signature[] signatures = SignatureUtil.getSignatureArray(context,
				pkgName);
		if (signatures.length > 0) {
			Signature sign = signatures[0];
			String signStr = sign.toCharsString();
			byte[] signature = sign.toByteArray();

			((TextView) findViewById(R.id.signature_self_info_md5))
					.setText(getString(
							R.string.signature_self_md5,
							CommonUtils.md5(signature) + "/"
									+ Md5Helper.MD5Encode(signature)));
			((TextView) findViewById(R.id.signature_self_info_sha1))
					.setText(getString(R.string.signature_self_sha1,
							CommonUtils.sha1(signature)));
			X509Certificate x509 = SignatureUtil.getX509Certificate(sign);
			((TextView) findViewById(R.id.signature_public_key))
					.setText(getString(
							R.string.signature_public_key,
							x509.getPublicKey().toString() + "\r\n"
									+ "Certificate.getType():" + x509.getType()
									+ "\r\n" + "Certificate.getVersion():"
									+ x509.getVersion() + "\r\n"
									+ "Certificate.getSerialNumber():"
									+ x509.getSerialNumber() + "\r\n"));
			((TextView) findViewById(R.id.signature_self_info))
					.setText(getString(R.string.signature_self, signStr));
		}
		((TextView) findViewById(R.id.signature_length)).setText(getString(
				R.string.signature_length, signatures.length));
	}

	/**
	 * 获取公钥
	 * 
	 * @param signature
	 * @return
	 */
	public static String getPublicKey(byte[] signature) {
		try {

			CertificateFactory certFactory = CertificateFactory
					.getInstance("X.509");
			X509Certificate cert = (X509Certificate) certFactory
					.generateCertificate(new ByteArrayInputStream(signature));

			String publickey = cert.getPublicKey().toString();
			publickey = publickey.substring(publickey.indexOf("modulus: ") + 9,
					publickey.indexOf("\n", publickey.indexOf("modulus:")));

			Log.d("TRACK", publickey);
			return publickey;
		} catch (CertificateException e) {
			e.printStackTrace();
		}
		return null;
	}

}
